package com.hds.modules.app.entity.wx.po;

import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

/**
 * 微信关单接口请求数据对象
 * @author guonan
 * @Date 2016-12-16
 */
public class CloseOrder {
    /**
     * 公众账号ID
     * */
    private String appid;

    /**
     * 商户号
     */
    private String mch_id;

    /**
     * 商户订单号
     */
    private String out_trade_no;

    /**
     * 随机字符串
     */
    private String nonce_str;

    /**
     * 签名
     */
    private String sign;

    @Override
    public String toString() {
        return "CloseOrder [" + (appid != null ? "appid=" + appid + ", " : "")
                + (mch_id != null ? "mch_id=" + mch_id + ", " : "")
                + (out_trade_no != null ? "out_trade_no=" + out_trade_no + ", " : "")
                + (nonce_str != null ? "nonce_str=" + nonce_str + ", " : "") + (sign != null ? "sign=" + sign : "")
                + "]";
    }

    public String getAppid() {
        return appid;
    }

    public void setAppid(String appid) {
        this.appid = appid;
    }

    public String getMch_id() {
        return mch_id;
    }

    public void setMch_id(String mch_id) {
        this.mch_id = mch_id;
    }

    public String getOut_trade_no() {
        return out_trade_no;
    }

    public void setOut_trade_no(String out_trade_no) {
        this.out_trade_no = out_trade_no;
    }

    public String getNonce_str() {
        return nonce_str;
    }

    public void setNonce_str(String nonce_str) {
        this.nonce_str = nonce_str;
    }

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    /**
     * 创建请求报文
     * @param closeOrder
     * @return
     * @throws DocumentException
     */

    public String creatRequestXML(CloseOrder closeOrder){
        String format ="<xml>"
                + "<appid><![CDATA[%1$s]]></appid>"
                + "<mch_id><![CDATA[%2$s]]></mch_id>"
                + "<out_trade_no><![CDATA[%3$s]]></out_trade_no>"
                + "<nonce_str><![CDATA[%4$s]]></nonce_str>"
                + "</xml>";

        String requestXML = String.format(format, new Object[] {
                ""+closeOrder.getAppid(),
                ""+closeOrder.getMch_id(),
                ""+closeOrder.getOut_trade_no(),
                ""+closeOrder.getNonce_str(),
                ""+closeOrder.getSign()
        });

        //清除所有空数据节点，放置微信端校验不过
        Document doc = null;

        try {
            doc = DocumentHelper.parseText(requestXML);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        Element root = doc.getRootElement();
        List attributes = root.elements();
        Iterator iter = attributes.iterator();
        while(iter.hasNext()){
            Element element = (Element)iter.next();
            if(element.getTextTrim().equals("null")){
                root.remove(element);
            }
        }
        requestXML = doc.asXML();
        System.out.println("请求XML数据:"+doc.asXML());
        return requestXML;
    }



}